package com.ssc.demo2;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Producer {
    private static final String BROKER_URL="tcp://49.234.233.179:61616";
    public static void main(String[] args) throws JMSException {
        //连接工厂
        ConnectionFactory connectionFactory=null;
        //连接实例
       Connection connection=null;
       //收发的线程实例
        Session session=null;
        //消息发送目标地址
        Destination destination=null;
        try{
            //实例化连接工厂
            connectionFactory=new ActiveMQConnectionFactory("ssc", "ssc", BROKER_URL);
            //获取连接实例
            connection=connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建接收或发送的线程实例
            session =connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
            //创建队列
            destination=session.createTopic("test1.topic");
            //创建消息发布者
            MessageProducer messageProducer=session.createProducer(destination);
            //创建TestMessage消息
            TextMessage message=session.createTextMessage("你好测试，这是发布的消息");
            messageProducer.send(message);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(session!=null){
                session.close();
            }
            if(connection!=null){
                connection.close();
            }
        }
    }
}
